﻿<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>Floodlight OpenFlow Controller - Switches</title>
    <!-- Bootstrap Core CSS -->
    <link href="../bower_components/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- MetisMenu CSS -->
    <link href="../bower_components/metisMenu/dist/metisMenu.min.css" rel="stylesheet">
    <!-- DataTables CSS -->
    <link href="../bower_components/datatables-plugins/integration/bootstrap/3/dataTables.bootstrap.css"
          rel="stylesheet">
    <!-- DataTables Responsive CSS -->
    <link href="../bower_components/datatables-responsive/css/dataTables.responsive.css" rel="stylesheet">
    <!-- Custom CSS -->
    <link href="../dist/css/sb-admin-2.css" rel="stylesheet">
    <!-- Custom Fonts -->
    <link href="../bower_components/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<div id="wrapper">
    <!-- Navigation -->
    <nav class="navbar navbar-default navbar-static-top" role="navigation" style="margin-bottom: 0">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="index.html" id="home-button-title">Floodlight OpenFlow Controller </a>
        </div>
        <!-- /.navbar-header -->
        <!-- /.navbar-top-links -->
        <div class="navbar-default sidebar" role="navigation">
            <div class="sidebar-nav navbar-collapse">
                <nav id="navMenu"></nav>
            </div>
            <!-- /.sidebar-collapse -->
        </div>
        <!-- /.navbar-static-side -->
    </nav>
    <div id="page-wrapper">
        <div class="row">
            <div class="col-lg-12">
                <h3 class="page-header">Switches</h3>
            </div>
            <!-- /.col-lg-12 -->
        </div>
        <!-- /.row -->
        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-primary">
                    <div class="panel-heading">
                        Switches Connected
                    </div>
                    <!-- /.panel-heading -->
                    <div class="panel-body">
                        <div class="dataTable_wrapper">
                            <table class="table table-striped table-bordered table-hover" id="tableSwitches"
                                   width="100%">
                                <thead>
                                <tr>
                                    <th>Switch ID</th>
                                    <th>IPv4 Address</th>
                                    <th>Connected Since</th>
                                </tr>
                                </thead>
                                <tbody></tbody>
                            </table>
                        </div>
                        <!-- /.table-responsive -->
                    </div>
                    <!-- /.panel-body -->
                </div>
                <!-- /.panel -->

            </div>
        </div>
        <div class="row">
            <!-- /.col-lg-8 -->
            <div class="col-lg-12">

                <div class="panel panel-primary">
                    <div class="panel-heading">
                        Switch Roles
                    </div>
                    <!-- /.panel-heading -->
                    <div class="panel-body">
                        <div class="dataTable_wrapper">
                            <div id="rolTable">
                            </div>
                        </div>
                        <!-- /.table-responsive -->
                    </div>
                    <!-- /.panel-body -->
                </div>

            </div>
        </div>
        <!-- /.row -->
    </div>
</div>
<!-- /#page-wrapper -->

<div id="login-modal-include"></div>
</div>
<!-- /#wrapper -->
<!-- jQuery -->
<script src="../bower_components/jquery/dist/jquery.min.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="../bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<!-- Metis Menu Plugin JavaScript -->
<script src="../bower_components/metisMenu/dist/metisMenu.min.js"></script>
<!-- DataTables JavaScript -->
<script src="../bower_components/datatables/media/js/jquery.dataTables.min.js"></script>
<script src="../bower_components/datatables-plugins/integration/bootstrap/3/dataTables.bootstrap.min.js"></script>
<!-- Custom Theme JavaScript -->
<script src="../dist/js/sb-admin-2.js"></script>
<script src="../js/jquery.cookie.js"></script>

<!-- Page-Level Demo Scripts - Tables - Use for reference -->
<script src="../js/querystringparser.js"></script>

<!-- Custom scripts to load in HTML -->
<script src="../js/navbar.js"></script>
<script>
    $(function () {
        $("#login-modal-include").load("loginmodal.html");
    });
</script>

<script>
    /*
     * These cookies are set on the login page at login.html.
     * They are simply the IP address of your controller and the REST api port.
     * */
    var ipaddress = $.cookie('cip');
    if (ipaddress == null || ipaddress == "") window.location.href = "login.html";
    var restport = $.cookie('cport');
    if (restport == null || restport == "") window.location.href = "login.html";

    $(document).ready(function () {
        loadSwitches();
        startRefresh();
    });

    /*
     * Refreshes all of the values on the page every fifteen seconds.
     * */
    function startRefresh() {
        setTimeout(startRefresh, 15000);

        switchTable.ajax.reload();
    }

    /*
     * Gets all of the connected switches from the controller.
     * */
    function loadSwitches() {
        switchTable = $('#tableSwitches').DataTable({
            responsive: true,
            searching: false,
            lengthChange: false,
            scrollX: true,
            paging: false,
            order: [[0, 'asc']],
            ajax: {
                url: "http://" + ipaddress + ":" + restport + "/wm/core/controller/switches/json",
                dataSrc: ''
            },
            columnDefs: [
                {
                    // The 'data' parameter refers to the data for the cell (defined by the
                    // 'data' option, which defaults to the column being worked with, in
                    // this case 'data: 0'.
                    "render": function (data, type, row) {
                        return "<a href='switchDetail.html?macAddress=" + data + "'>" + data + "</<a>";
                    },
                    "targets": 0
                },
                {
                    "render": function (data, type, row) {
                        var t = new Date(1970, 0, 1); // Epoch
                        t.setSeconds(data / 1000);

                        return t;
                    },
                    "targets": 2
                }
            ],
            columns: [
                {data: 'switchDPID'},
                {data: 'inetAddress'},
                {data: 'connectedSince'}
            ]
        });
    }

    $(document).ready(function () {

        $.ajax({
            url: "http://" + ipaddress + ":" + restport + "/wm/core/switch/all/role/json",
            success: function (data) {
                var obj = data;
                var html = " <table class='table table-bordered table-striped table-hover responsive-utilities'>" +
                        "<thead><tr> <th>Switch MAC </th>  <th>Role</th></tr></thead><tbody>";

                for (propName in obj) {
                    if (obj.hasOwnProperty(propName)) {
                        value = obj[propName];
                    }
                    html += "<tr><td>" + propName + "</td><td>" + value + "</td></tr>";
                }

                html += "  </tbody></table>";

                $("#rolTable").html("");
                $("#rolTable").html(html);

            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert('Error: ' + " " + jqXHR.responseText + " \n Status: " + textStatus + " \n Error Thrown: " + errorThrown);
            }
        });

    });

</script>
</body>
</html>
